function setFixed(id) {
	var elem = document.getElementById(id);
	if (!elem) return;
	
	var style = elem.style, doc = document.documentElement || document.body,
		isIE6 = ($.browser.msie && $.browser.version < 7), left, top;
	
	if (isIE6) {
		style.position = 'absolute';
		
		left = parseInt(elem.offsetLeft) - doc.scrollLeft,
		top  = parseInt(elem.offsetTop)  - doc.scrollTop;
		
		style.removeExpression('left');
		style.removeExpression('top');
		style.removeExpression('right');
		style.removeExpression('bottom');
		
		style.setExpression('left', 'this.ownerDocument.documentElement.scrollLeft' + left);
		style.setExpression('top', 'this.ownerDocument.documentElement.scrollTop + ' + top);
		
		if (document.body.style.backgroundAttachment != 'fixed') {
			document.body.style.backgroundImage = "url('http://static.test.com/style/v1/images/common/bg_fixed.gif')";
			document.body.style.backgroundAttachment = 'fixed';
		}
	} else {
		style.position = 'fixed'; 
	}
}

function noFlashRightClick(pid) {
	var el = document.getElementById(pid);
	if(el.addEventListener) {
		el.addEventListener('mousedown', function(event) {
			if(event.button == 2) {
				event.stopPropagation(); //for firefox
				event.preventDefault();  //for chrome
			}
		}, true);
	} else {
		el.attachEvent('onmousedown', function() {
			if(event.button == 2) {
				el.setCapture();
			}
		});
		
		el.attachEvent("onmouseup",function(){
			el.releaseCapture();
		});
		
		el.oncontextmenu = function(){
			return false;
		};
	}
}

function compileTemplate(template, vals) {
	var html = template;
	for (index in vals) {
		html = html.replace('{$'+ index +'}', vals[index]);
	}
	return html;
}

(function($) {
	$.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + options.path : '';
        var domain = options.domain ? '; domain=' + options.domain : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
	}
})(jQuery);

function updateShippingCartItemCount() {
	if (g_globals.objects.cart) {
		g_globals.objects.cart.updateCartItemCount();
	}
}

g_globals.dollarExchangeRate = {
	usd: 1,
	eur: 0.7056,
	gbp: 0.622,
	aud: 0.9372,
	cad: 0.9926,
	jpy: 75.853,
	cny: 6.3559,
	hkd: 7.7713
};

var web = web || {};
web.Currency = Class.create(); 
web.Currency.prototype = {
	initialize: function(options) {
		var _this = this;
		this.currencys = {
			usd: {s: '$'},
			eur: {s: '€'},
			gbp: {s: '£'},
			aud: {s: '$'},
			cad: {s: 'C$'},
			jpy: {s: '¥'},
			cny: {s: '¥'},
			hkd: {s: 'HK$'}
		};
		
		this.currentlyCurrency = $.cookie('currency');
		if (!this.currentlyCurrency) {
			this.currentlyCurrency = 'usd';
		}
		
		$('#atc-drop-currencies').find('a').bind('click', function(){
			var a = $(this);
			_this.currentlyCurrency = a.attr('currencie');
			_this.converterToSettingCurrency();
			$.cookie('currency', _this.currentlyCurrency);
		});
	},
	converterToSettingCurrency: function(parent) {
		var _this = this;
		var p = parent || null;
		var elems = (p == null) ? $('span.money') : $('span.money', p);
		var currencyType = this._getSettingCurrencyType();
		var exchangeRate = this._getDollarExchangeRate(currencyType);
		if (!this.currencys[currencyType]) return;
		
		elems.each(function(){
			var money = $(this);
			var dollar = Number(money.attr('us-dollar'));
			if (!isNaN(dollar)) {
				var c = currencyType.toUpperCase();
				var v = (dollar * exchangeRate).toFixed(2);
				var s = _this.currencys[currencyType]['s'];
				var p = '<span class="symbol">'+ s +'</span><span class="num">'+ v +'</span>';
				if (money.attr('noname')) {
					money.html(p);
				} else {
					money.html('<span class="name">'+ c +'</span>'+ p);
				}
			}
		});
	},
	_getSettingCurrencyType: function() {
		return this.currentlyCurrency;
	},
	_getDollarExchangeRate: function(currencyType) {
		return g_globals.dollarExchangeRate[this.currentlyCurrency];
	}
};

(function($) {
	$.fn.smartFloat = function() {
		var position = function(element) {
			
			var isIE6 = ($.browser.msie && $.browser.version < 7);
			if (isIE6) {
				if (document.body.style.backgroundAttachment != 'fixed') {
					document.body.style.backgroundImage = "url('http://static.test.com/style/v1/images/common/bg_fixed.gif')";
					document.body.style.backgroundAttachment = 'fixed';
				}
			}
			
			var top = element.position().top, pos = element.css("position");
			$(window).scroll(function() {
				var scrolls = $(this).scrollTop();
				if (scrolls > top) {
					if (window.XMLHttpRequest) {
						element.css({position: "fixed", top: 0});    
					} else {
						element.css({top: scrolls});    
					}
				} else {
					element.css({position: "absolute", top: top});    
				}
			});
		};
		
		return this.each(function() {
			position($(this));                         
		});
	};
})(jQuery);

var web = web || {};
web.QuickPreview = Class.create(); 
web.QuickPreview.prototype = {
	initialize: function(options) {
		var _this = this;
		this.settings = $.extend({
			view_url: 'preview-url'
		}, options);
		
		this.currentlyElement = null;
		this.panelCloseTimer = null;
		this.panel = null;
		this.previewIns = null;
		this.previewLoading = null;
		this.previewResult = null;
		
		this._init();
	},
	preview: function(divs) {
		var _this = this;
		return divs.each(function() {
			var element = $(this);
			element.bind({
				mouseenter: function(){
					_this._toLoadPreview(element);
					return false;
				}, 
				mouseleave: function(){
					_this._hidePanel();
					return false;
				}
			});
		});
	},
	_init: function() {
		this.panel = $('#div-quick-preview-panel');
		if (0 == this.panel.size()) {
			this._createPanel();
		}
	},
	_clearPanelCloseTimer: function() {
		if (this.panelCloseTimer) {
			window.clearTimeout(this.panelCloseTimer);
			this.panelCloseTimer = null;
		}
	},
	_showPanel: function() {
		this._clearPanelCloseTimer();
		this.panel.show();
	},
	_hidePanel: function() {
		var _this = this;
		this.panelCloseTimer = window.setTimeout(function(){
			_this.panel.hide();
		}, 200);
	},
	_showLoading: function() {
		this.previewResult.hide();
		this.previewLoading.show();
	},
	_showResult: function() {
		this.previewLoading.hide();
		this.previewResult.show();
	},
	_getViewUrl: function(element) {
		return element.attr(this.settings.view_url);
	},
	_toLoadPreview: function(element) {
		this.currentlyElement = element;
		var url = this._getViewUrl(element);
		if (!url) return;
		
		var pos = element.offset();
		var size = {w: element.innerWidth(), h: element.innerHeight()};
		this.panel.css({left: (pos.left + size.w), top: pos.top});
		this.previewIns.css('margin-top', parseInt(size.h/2));
		
		var states = element.attr('loaded');
		if ('ok' != states) {
			this._showLoading();
			if (0 == element.children('.quick-preview-cache').size()) {
				this._createCache(element, url);
			}
		} else {
			this._loadedFinish(element, url);
		}
		
		this._showPanel();
	},
	_createCache: function(element, url) {
		var _this = this;
		var isImg = true;
		
		if (isImg) {
			var div = $('<div class="quick-preview-cache"><img /></div>').hide();
			var img = div.children('img');
			img.bind('load', function(){
				_this._loadedFinish(element, url);
			});
			
			div.appendTo(element);
			img.attr('src', url);
		}
	},
	_loadedFinish: function(element, url) {
		element.attr('loaded', 'ok');
		if (this._getViewUrl(this.currentlyElement) == url) {
			this.previewResult.html(element.children('.quick-preview-cache').html());
			this._showResult();
		}
	},
	_createPanel: function() {
		var _this = this;
		var html = ''+
					'<div id="div-quick-preview-panel" class="quick-preview-panel">'+
					'	<div class="icon i-prview-arrow view-ins"></div>'+
					'	<div class="view-main"><div class="loading"></div><div class="result"></div></div>'+
					'</div>';
		this.panel = $(html);
		this.previewIns = this.panel.children('.view-ins');
		this.previewLoading = this.panel.find('.loading');
		this.previewResult = this.panel.find('.result');
		
		this.panel.bind({
			mouseenter: function(){_this._clearPanelCloseTimer();}, 
			mouseleave: function(){_this._hidePanel();}
		});
			
		this.panel.appendTo($(document.body));
	}
};

(function($) {
	$.fn.quickPreview = function() {
		var p = new web.QuickPreview();
		p.preview(this);
	};
})(jQuery);

